package com.bub.pay.framework.common.entity.model;

import com.alibaba.fastjson.annotation.JSONType;
import com.bub.pay.framework.common.exception.GlobalException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@JSONType(orders = {"code", "message", "data"})
@ApiModel("响应结果")
public class RespObject<T> implements Serializable {

    private static final long serialVersionUID = 3770020405205035916L;

    public static final String SUCCESS_MSG = "操作成功";
    public static final Integer SUCCESS_CODE = 200;

    @ApiModelProperty("编码")
    public Integer code;

    @ApiModelProperty("数据")
    public T data;

    @ApiModelProperty("提示消息")
    public String message;

    public RespObject() {
        this(SUCCESS_CODE, SUCCESS_MSG);
    }

    public RespObject(GlobalException ge) {
        this.code = ge.getCode();
        this.message = ge.getInfo();
    }

    public RespObject(T data) {
        this(SUCCESS_CODE, SUCCESS_MSG, data);
    }

    public RespObject(Integer code, String message) {
        this(code, message, null);
    }

    public RespObject(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public String getMessage() {
        return message == null || message.equals("") ? SUCCESS_MSG : message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getCode() {
        return code == null ? 200 : code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
